
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/md5_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/comm_message_gradient_dialog.dart';
import 'package:baselib/widgets/dialog_util.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/nim_sdk_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/user_entity.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:module_business/module_my/model/func_model.dart';
import 'package:module_business/module_my/service/module_my_service.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class MyViewModel extends BaseViewModel<ModuleMyService>{

  ModuleMyService moduleMyService;
  RefreshController refreshController = RefreshController(initialRefresh: true);
  RefreshState loadStatus=RefreshState.OK;
  int page = 1;
  bool isCanMoreData = false;
  List<dynamic> dataLists=[];
  //功能列表
  List<FuncModel> funcModels = [];
  UserEntity myInfo = UserEntity();
  @override
  ModuleMyService createService() =>moduleMyService = ModuleMyService();
  @override
  void onInit() {
    LogUtil.d('MyViewModel-->onInit');
    super.onInit();
    //设置初始化数据
    setInitData();
  }
  setInitData(){
    funcModels=[
      FuncModel(preImgPath: DrawableUtil.instance.icon_person_my_collection,title: StringUtil.instance.My_collection),
      FuncModel(preImgPath: DrawableUtil.instance.icon_person_administrator_room,title: StringUtil.instance.as_Administrator),
      FuncModel(preImgPath: DrawableUtil.instance.icon_person_browse_records,title: StringUtil.instance.Browse_Records),
      FuncModel(preImgPath: DrawableUtil.instance.icon_person_user_agreement,title: StringUtil.instance.User_Agreement),
      FuncModel(preImgPath: DrawableUtil.instance.icon_person_privacy_agreement,title: StringUtil.instance.Privacy_Agreement),
    ];
    myInfo = UserParamsModel.instance.userEntity;
  }

  onRefresh({int pageCur,}) async{
    BaseEntity baseEntity = await moduleMyService.api_user_getUserInfo();
    if(refreshController!=null)refreshController.refreshCompleted();
    if(refreshController!=null)refreshController.loadComplete();
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      myInfo = baseEntity?.result??UserEntity();
      UserParamsModel.instance.setUserEntity(myInfo);
      update();
    }
  }

  toProfileEdit() {
    NavigatorUtil.pushByName(RouteConfig.profileEditPage);
  }

  onFuncItemTap(String tag) {
    //type	类型(1：查看好友列表，2：查看粉丝列表，3：查看关注列表)
    String route = '';
    Map<String,dynamic> arguments={};
    if(tag==StringUtil.instance.Fans){
      route = RouteConfig.myFansPage;
      arguments={
        'title': tag,
        'type': '2',
      };
    }
    if(tag==StringUtil.instance.Follow){
      route = RouteConfig.myFansPage;
      arguments={
        'title': tag,
        'type': '3',
      };
    }
    if(tag==StringUtil.instance.Friends){
      route = RouteConfig.myFansPage;
      arguments={
        'title': tag,
        'type': '1',
      };
    }
    if(tag==StringUtil.instance.Visitor){
      route = RouteConfig.myFansPage;
      arguments={
        'title': tag,
        'type': '4',
      };
    }

    if(tag==StringUtil.instance.My_collection)route = RouteConfig.myCollectionPage;
    if(tag==StringUtil.instance.Browse_Records)route = RouteConfig.myBrowseRecordsPage;
    if(tag==StringUtil.instance.My_Banlance)route = RouteConfig.rechargePage;
    if(route.isEmpty)return;
    NavigatorUtil.pushByName(route,arguments: arguments);
  }

  deleteAccount() {
    showDialog(
        context: Get.context,
        builder: (_){
          return CommMessageGradientDialog(
            message: '${StringUtil.instance.Delete_Account_point}?',
            onPositivePressEvent: (){
              LogUtil.d('showDialog-->onPositivePressEvent');
              Navigator.of(Get.context).pop();
              startDeleteAccount();
            },
            onCloseEvent: (){
              LogUtil.d('showDialog-->onCloseEvent');
              Navigator.of(Get.context).pop();
            },
          );
        }
    );
  }

  void startDeleteAccount() async{
    LoadUtil.showLoad(loadingText: StringUtil.instance.submiting);
    Map<String,dynamic> parmsMap= {};
    BaseEntity baseEntity = await moduleMyService.api_user_deleteAccount(parmsMap: parmsMap);
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      await NIMSDKUtil.instance.logout();
      await UserParamsModel.instance.clear();
      LoadUtil.hideLoad();
      ToastUtil.toast(StringUtil.instance.Delete_account_success);
      NavigatorUtil.offAllByName(RouteConfig.loginPage);
    }else{
      LoadUtil.hideLoad();
      ToastUtil.toast(baseEntity?.message);
    }
  }

  logOut() {
    showDialog(
        context: Get.context,
        builder: (_){
          return CommMessageGradientDialog(
            message: '${StringUtil.instance.logOut_point}?',
            onPositivePressEvent: (){
              LogUtil.d('showDialog-->onPositivePressEvent');
              Navigator.of(Get.context).pop();
              startLogOut();
            },
            onCloseEvent: (){
              LogUtil.d('showDialog-->onCloseEvent');
              Navigator.of(Get.context).pop();
            },
          );
        }
    );
  }

  void startLogOut() async{
    LoadUtil.showLoad(loadingText: '${StringUtil.instance.logOuting}...');
    await NIMSDKUtil.instance.logout();
    await UserParamsModel.instance.clear();
    await Future.delayed(Duration(milliseconds: 500));
    LoadUtil.hideLoad();
    ToastUtil.toast(StringUtil.instance.Delete_account_success);
    NavigatorUtil.offAllByName(RouteConfig.loginPage);
  }

}
